﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.Controls.Base;
using Silvermoon.Animations;

namespace FileExplorer
{
    /// <summary>
    /// This is a helper class that has only one instance with the purpose to perform a transition of all items between edit mode and browse mode.
    /// </summary>
    public class FileItemMode
    {
        private static FileItemMode instance = new FileItemMode();

        public static FileItemMode Modes { get { return instance; } }

        private bool editMode;

        public bool Animating { get; private set; }

        public TreeView TreeView { get; set; }
        public bool EditMode
        {
            get { return editMode; }
            set { if (editMode != value) { SetEditMode(value); } }
        }

        private List<FileItem> items = new List<FileItem>();


        /// <summary>
        /// Occurs when the EditMode changes.
        // we perform an animation when we change the edit mode to fade from one state to the other:
        /// </summary>
        private void SetEditMode(bool value)
        {
            editMode = value;
            // if the treeview shows the root level, we cannot edit and return:
            if (TreeView.Page.ParentPage == null) return;

            if (value) editMode = value;

            // we get the listview that holds the FileItems that are displayed here:
            ListViewBase lvb = TreeView.Page.Control as ListViewBase;
            if (lvb != null)
            {
                // first we invalidate each item so that it changes it's appearance:
                TreeView.Page.Button.IsDown = value;
                Animating = true;

                // we access and iterate through every available item in the listview:
                foreach (Control c in lvb.Items.GetAvailableItems())
                {
                    FileItem item = c as FileItem;
                    if (item != null)
                    {
                        item.Invalidate(InvalidationFlag.LayoutAppearance);
                    }
                }
            }
        }
    }
}
